package com.mxprojects.views
{
	import com.mxprojects.admin.AdminThreads;
	import com.mxprojects.constructors.ButtonLarge;
	import com.mxprojects.gui.BreadCrumbs;
	import com.mxprojects.gui.DefaultDialog;
	import com.mxprojects.gui.ForumDropdown;
	import com.mxprojects.gui.PageNav;
	import com.mxprojects.gui.PostFooter;
	import com.mxprojects.gui.PostHeader;
	import com.mxprojects.gui.PostItem;
	import com.mxprojects.gui.Resizer;
	import com.mxprojects.io.BoardCookie;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.CustomEvent;
	import com.mxprojects.io.DeepLinking;
	import com.mxprojects.io.Permissions;
	import com.mxprojects.io.ServerHandler;
	import com.mxprojects.io.UserControl;
	import com.mxprojects.language.Language;
	import com.mxprojects.language.Word;
	
	import flash.events.MouseEvent;
	
	public class Posts
	{
		public static var isReadOnly:int;
		public static var postData:Object;
		
		private static var loadPosts:ServerHandler = new ServerHandler();
		private static var postHeader:PostHeader = new PostHeader();
		private static var postFooter:PostFooter = new PostFooter();
		private static var newThreadButton:ButtonLarge;
		private static var replyPostButton:ButtonLarge;
		private static var forumDropdown:ForumDropdown;
		private static var adminThreadButton:AdminThreads;
				
		public function Posts(){}
		
		public static function load(threadID:int, page:int = 1):void {
			//get the posts
			var dataToSend:Object = new Object();
			dataToSend.threadID = threadID;
			dataToSend.page = page;
			dataToSend.postsPerPage = Config.POSTS_PER_PAGE;
			dataToSend.webImgDir = Config.INSTALL_DIRECTORY + Config.IMAGE_DIRECTORY + Config.POST_UPLOAD_SUB_DIRECTORY;
			dataToSend.abImgDir = Config.ABSOLUTE_INSTALL_PATH + Config.IMAGE_DIRECTORY + Config.POST_UPLOAD_SUB_DIRECTORY;
			dataToSend.avatarDir = Config.INSTALL_DIRECTORY + Config.IMAGE_DIRECTORY + Config.AVATAR_UPLOAD_SUB_DIRECTORY;
			dataToSend.guestGroupID = Config.GUEST_GROUP_ID;
			
			//if the user is logged in let's send the username along so that we can track threads
			if(BoardCookie.getUser() != "undefined" && BoardCookie.getUser() != null){
				dataToSend.username = BoardCookie.getUser();
				dataToSend.password = BoardCookie.getPassword();
			}
			
			loadPosts.transmitData("viewposts.php", dataToSend);
			loadPosts.listenForData(showPosts);
			
			//clear the board until it's loaded
			MXP3.clearBoard();
		}
		
		private static function showPosts(event:CustomEvent):void {
			//get the category data
			postData = event.getServerData();
			
			//see if there was some oddness with the login
			if(postData.output == "badLogin"){
				DefaultDialog.show(Language.main(Word.ERROR_LOGIN_INVALID));
				return;
			}
			
			//make sure we are even showing a post
			if(postData.output == "threadNotFound"){
				DefaultDialog.show(Language.main(Word.ERROR_THREAD_NOT_FOUND), true, Word.ERROR, Permissions.returnToHome);
				return;
			}
			
			//make sure they are allowed to be here
			if(postData.output == "permissionError"){
				Permissions.error();
				return;
			}
			
			//set the current permissions
			Permissions.setup(postData.isAdmin, postData.isModerator, postData.isAllowedToPost);
			isReadOnly = postData.threadReadMode;
			
			var nextY:int = 0;
			
			//tell the board where we are
			MXP3.currentLocation = Word.POSTS;
			PageNav.createNav(postData.currentPage, postData.totalPages, postData.threadID);
			BreadCrumbs.createCrumbs(postData.forumID, postData.forumTitle, postData.threadID, postData.threadTopic);
			DeepLinking.setTitle(postData.threadTopic);
			
			//put the thread header up
			postHeader.y = nextY;
			postHeader.resize();
			nextY += postHeader.height - Config.DEFAULT_LINE_HEIGHT;
			MXP3.addContent(postHeader);
						
			//loop through the posts
			for(var i:uint = 0; i < postData.postCount; i++){
				var post:PostItem = new PostItem();

				//add any images to the message
				for(var img:int = 0; img < int(postData["post"+i+"totalImages"]); img++){
					//add them to the post
					post.addImage(postData["post"+i+"image"+img+"Location"], 
								  postData["post"+i+"image"+img+"Description"],
								  postData["post"+i+"image"+img+"Width"],
								  postData["post"+i+"image"+img+"Height"]);
				}
				
				post.display(postData["post"+i+"postID"],
					postData["post"+i+"message"],
					postData["post"+i+"author"],
					postData["post"+i+"authorID"],
					postData["post"+i+"userTitle"],
					postData["post"+i+"posts"],
					postData["post"+i+"joined"],
					postData["post"+i+"date"],
					postData["post"+i+"avatarURL"],
					postData["post"+i+"signature"],
					postHeader);
				
				post.y = nextY;
				post.name = "post" + i;
				nextY += post.height - Config.DEFAULT_LINE_HEIGHT;
				MXP3.addContent(post);
			}
			
			//put the footer on
			postFooter.y = nextY;
			postFooter.resize();
			nextY += postFooter.height - Config.DEFAULT_LINE_HEIGHT;
			MXP3.addContent(postFooter);
						
			//add the dropdown menu
			forumDropdown = new ForumDropdown(postData.forumID);
			forumDropdown.x = Config.TEXT_PADDING;
			forumDropdown.y = nextY + Config.TEXT_PADDING;
			MXP3.addContent(forumDropdown);
			
			//add post new and post reply buttons
			newThreadButton = new ButtonLarge(Language.main(Word.NEW_THREAD), newThread);
			newThreadButton.y = nextY + Config.TEXT_PADDING;
			newThreadButton.x = MXP3.wStage - newThreadButton.width - Config.TEXT_PADDING;
			MXP3.addContent(newThreadButton);
			
			replyPostButton = new ButtonLarge(Language.main(Word.POST_REPLY), postReply);
			replyPostButton.y = nextY + Config.TEXT_PADDING;
			replyPostButton.x = Math.round(MXP3.wStage - newThreadButton.width - (Config.TEXT_PADDING * 2) - replyPostButton.width);
			MXP3.addContent(replyPostButton);
			
			//setup the default permissions as being good to go
			setReplyPostButton(true);
			setNewThreadButton(true);
			
			//check permissions
			if(isReadOnly == 1 && postData.isAdmin == 0 && postData.isModerator == 0 || postData.isAllowedToPost == 0){
				setReplyPostButton(false);
			}
			
			if(Threads.isForumRestricted == 1 && postData.isAdmin == 0 && postData.isModerator == 0 || postData.isAllowedToPost == 0){
				setNewThreadButton(false);
			}
			
			//focus the stage so the backspace works
			newThreadButton.stage.focus = newThreadButton.stage;
			
			//resize the page if we need to
			Resizer.resizeContent();
		}
		
		public static function setNewThreadButton(isEnabled:Boolean):void {
			//this just allows the new thread button to become active or not
			newThreadButton.enabled = isEnabled;
			
			//throw an admin button on there if need be
			if(Permissions.isAdmin == 1 || Permissions.isModerator == 1){
				//set the post type vars
				var isSticky:Boolean = false;
				var isLocked:Boolean = false;
				var isAnnoucement:Boolean = false;
				
				if(postData.threadDisplayOrder == 1){
					isSticky = true;
				}
				
				if(postData.threadDisplayOrder == Config.ANNOUCEMENT_DISPLAY_ORDER){
					isAnnoucement = true;
				}
				
				if(postData.threadReadMode == 1){
					isLocked = true;
				}
				
				//create a button to open the thread admin
				adminThreadButton = new AdminThreads();
				adminThreadButton.display(int(postData.forumID), int(postData.threadID), postData.threadTopic, 
										  isSticky, isLocked, isAnnoucement, postData.currentPage);
				MXP3.addContent(adminThreadButton);
				
				//do some crazy stuff to move the button around
				adminThreadButton.x = Math.round(replyPostButton.x - adminThreadButton.width - Config.TEXT_PADDING);
				adminThreadButton.y = Math.round(replyPostButton.y);					
			}
		}
		
		public static function setReplyPostButton(isEnabled:Boolean):void {
			//this just allows the new thread button to become active or not
			replyPostButton.enabled = isEnabled;
		}
		
		public static function setPostOptions(isEnabled:Boolean):void {
			for(var i:uint = 0; i < postData.postCount; i++){
				var post:PostItem = MXP3.getContent("post"+i);
				post.postOptions.setupButtons(isEnabled);
			}
		}
		
		public static function postReply(event:MouseEvent):void {
			if(UserControl.loggedIn){
				CreatePost.create(CreatePost.ACTION_POST_REPLY, postData.forumID, postData.forumTitle, postData.threadID, postData.threadTopic);
			}else{
				LoginDialog.show();
			}
		}
		
		public static function newThread(event:MouseEvent):void {
			if(UserControl.loggedIn){
				CreatePost.create(CreatePost.ACTION_POST_NEW, postData.forumID, postData.forumTitle);
			}else{
				LoginDialog.show();
			}
		}
		
		public static function resize():void {			
			var nextY:int = postHeader.height - Config.DEFAULT_LINE_HEIGHT;

			postHeader.resize();
			postFooter.resize();
						
			for(var i:uint = 0; i < postData.postCount; i++){
				var post:PostItem = MXP3.getContent("post"+i);
				post.resize();
				post.y = nextY;
				nextY += Math.floor(post.height - Config.DEFAULT_LINE_HEIGHT);
			}
			
			postFooter.y = nextY;
			nextY += postFooter.height - Config.DEFAULT_LINE_HEIGHT;
			
			//move the buttons
			newThreadButton.y = nextY + Config.TEXT_PADDING;
			newThreadButton.x = MXP3.wStage - newThreadButton.width - Config.TEXT_PADDING;
			
			replyPostButton.y = nextY + Config.TEXT_PADDING;
			replyPostButton.x = Math.round(MXP3.wStage - newThreadButton.width - (Config.TEXT_PADDING * 2) - replyPostButton.width);
			
			//if the dropdown is around, move that as well
			if(forumDropdown != null){
				forumDropdown.y = postFooter.y + postFooter.height + Config.TEXT_PADDING;
			}
			
			//move the thread admin button if it's there
			if(adminThreadButton != null){
				//do some crazy stuff to move the button around
				adminThreadButton.x = Math.round(replyPostButton.x - adminThreadButton.width - Config.TEXT_PADDING);
				adminThreadButton.y = Math.round(replyPostButton.y);
			}
		}
	}
}